package com.ruoyi.mall.product.domain.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mall.product.domain.service.IPmsSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.product.domain.repository.PmsSkuMapper;
import com.ruoyi.mall.product.domain.entity.PmsSku;

/**
 * sku信息Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Service
public class PmsSkuServiceImpl implements IPmsSkuService {
    @Autowired
    private PmsSkuMapper pmsSkuMapper;

    /**
     * 查询sku信息
     *
     * @param id sku信息主键
     * @return sku信息
     */
    @Override
    public PmsSku selectPmsSkuById(Long id) {
        return pmsSkuMapper.selectById(id);
    }

    /**
     * 查询sku信息列表
     *
     * @param pmsSku sku信息
     * @return sku信息
     */
    @Override
    public List<PmsSku> selectPmsSkuList(PmsSku pmsSku) {

        QueryWrapper<PmsSku> wrapper = new QueryWrapper<>();
        String key = pmsSku.getKey();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(i -> {
                i.eq("id", key).or().like("sku_name", key);
            });
        }
        Long brandId = pmsSku.getBrandId();
        if (!("0".equals(String.valueOf(brandId)) || "null".equals(String.valueOf(brandId)))) {
            wrapper.eq("brand_id", brandId);
        }

        Long categoryId = pmsSku.getCategoryId();
        if (!("0".equals(String.valueOf(categoryId)) || "null".equals(String.valueOf(categoryId)))) {
            wrapper.eq("category_id", categoryId);
        }

        Integer min = pmsSku.getMin();
        if (!("0".equals(String.valueOf(min)) || "null".equals(String.valueOf(min)))) {
            wrapper.ge("price", min);
        }


        Integer max = pmsSku.getMax();
        if (!("0".equals(String.valueOf(max)) || "null".equals(String.valueOf(max)))) {
            wrapper.le("price", max);
        }
        return pmsSkuMapper.selectList(wrapper);
    }

    /**
     * 新增sku信息
     *
     * @param pmsSku sku信息
     * @return 结果
     */
    @Override
    public int insertPmsSku(PmsSku pmsSku) {
        return pmsSkuMapper.insert(pmsSku);
    }

    /**
     * 修改sku信息
     *
     * @param pmsSku sku信息
     * @return 结果
     */
    @Override
    public int updatePmsSku(PmsSku pmsSku) {
        return pmsSkuMapper.updateById(pmsSku);
    }

    /**
     * 批量删除sku信息
     *
     * @param ids 需要删除的sku信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSkuByIds(Long[] ids) {
        return pmsSkuMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除sku信息信息
     *
     * @param id sku信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSkuById(Long id) {
        return pmsSkuMapper.deleteById(id);
    }
}
